package com.luo.chengrui.jasperprinter;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimplePrintServiceExporterConfiguration;

import javax.print.*;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @version 1.0.0
 * @description
 * @createTime 2024/07/26
 */
public class PrintDemo {
    public static void main(String[] args) {
        exportToPDF();
    }

    private static void getPrinter() {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println(Arrays.stream(printServices).map(PrintService::getName).collect(Collectors.toList()));
        Arrays.stream(printServices).forEach(item -> {
            System.out.println(item.getName());
        });

        Map<String, Object> result = new HashMap<>();
        result.put("data", Arrays.stream(printServices).map(PrintService::getName).collect(Collectors.toList()));
        System.out.println(result);
    }

    private static void print() {
        // 获取默认的打印服务
        PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
        if (defaultPrintService == null) {
            System.out.println("无可用的打印服务");
            return;
        }

        System.out.println("默认打印机: " + defaultPrintService.getName());

        try {
            // 读取文件内容
            FileInputStream fis = new FileInputStream("D:\\TEST\\50_人员条码.pdf");
            // 创建打印任务
            DocPrintJob docPrintJob = defaultPrintService.createPrintJob();
            // 构建打印文档
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            Doc doc = new SimpleDoc(fis, flavor, null);
            // 执行打印任务
//            docPrintJob.print(doc, null);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void jasperPrinter() {
        try {

            Map<String, Object> jasperPamars = new HashMap<>();
            JasperPrint print = JasperFillManager.fillReport("D:\\JapserWorkspace\\WmsReport\\reportFile\\人员条码.jasper", jasperPamars, (Connection) null);
            JasperPrint print2 = JasperFillManager.fillReport("D:\\JapserWorkspace\\WmsReport\\reportFile\\人员条码.jasper", jasperPamars, (Connection) null);

            System.out.println("报表页数: " + print.getPages());
            JasperExportManager.exportReportToPdfFile(print, "D:\\TEST\\人员条码.pdf");

            // 获取默认的打印服务
            PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
            if (defaultPrintService == null) {
                System.out.println("无可用的打印服务");
                return;
            }
            System.out.println("默认打印机: " + defaultPrintService.getName());
            JRPrintServiceExporter printServiceExporter = new JRPrintServiceExporter();
            printServiceExporter.setConfiguration(new SimplePrintServiceExporterConfiguration() {

                @Override
                public PrintService getPrintService() {
                    return defaultPrintService;
                }
            });
            printServiceExporter.setExporterInput(new SimpleExporterInput(print));
            printServiceExporter.exportReport(); // 执行打印任务
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("生成打印表单打败" + ex.getMessage());
        }
    }

    private static void exportToPDF() {
        try {

            Map<String, Object> jasperPamars = new HashMap<>();
            jasperPamars.put("a", "张三");
            JasperPrint print = JasperFillManager.fillReport("D:\\JapserWorkspace\\WmsReport\\reportFile\\TEST.jasper", jasperPamars, (Connection) null);

            JasperExportManager.exportReportToPdfFile(print, "D:\\TEST\\人员条码.pdf");
            jasperPamars.put("a", "李四");
            JasperPrint print2 = JasperFillManager.fillReport("D:\\JapserWorkspace\\WmsReport\\reportFile\\TEST.jasper", jasperPamars, (Connection) null);
            JasperExportManager.exportReportToPdfFile(print2, "D:\\TEST\\人员条码.pdf");

            System.out.println("报表页数: " + print.getPages());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException("生成打印表单打败" + ex.getMessage());
        }
    }
}
